<?php
declare(strict_types=1);

namespace Modules\Shop\Service;

use DB;
use HiCommon\Repository\RepairstoreRepository;
use HiCommon\Repository\RepairstoreGoodsRepository;
use HiCommon\Repository\RepairstoreGoodsImgRepository;
use HiCommon\Repository\RepairstoreTypeRepository;
use HiCommon\Repository\ShopRepository;

class RepairstoreService
{

    /**
     * 商品列表
     */
    public function getGoodsList($where)
    {
        $admin = shop_user();//当前管理员信息
        $where['repairstore_id'] = $admin->shop_id;

        $data = RepairstoreGoodsRepository::get_list($where);

        $data->appends($where);//添加链接参数
        $list = list_page_data($data, $where);
        $list['type'] = self::getGoodsType();

        return $list;
    }

    /**
     * 商品类型列表
     */
    public function getGoodsType()
    {
        return RepairstoreTypeRepository::get_all(['status' => 1], ['id', 'title']);
    }

    /**
     * 添加商品
     */

    public function addOne(array $arrInput)
    {
        try {
            unset($arrInput['file']);
            $shop = shop_user();//当前管理员信息
            $arr_option['field'] = ['id', 'longitude', 'latitude'];
            $info = ShopRepository::base_find_one($shop->shop_id, $arr_option);

            $arrInput['repairstore_id'] = $info->id;
            $arrInput['longitude'] = $info->longitude;
            $arrInput['latitude'] = $info->latitude;

            $imgs = $arrInput['img'];
            unset($arrInput['img']);

            $res = RepairstoreGoodsRepository::add_one($arrInput);

            $imgArray = array();
            foreach ($imgs as $key => $value) {
                $imgArray['goods_id'] = $res->id;
                $imgArray['picurl'] = $value;
                $imgArray['sorts'] = $key + 1;
                RepairstoreGoodsImgRepository::add_one($imgArray);//存入图片信息
            }

            if (empty($res)) {
                return hello_error('添加失败');
            }

            return hello_success('添加成功', $res);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     * 获取一条数据
     */
    public function getOne(int $id)
    {
        $data = RepairstoreGoodsRepository::get_one($id);
        if ($data) {
            return $data->toarray();
        }
        return [];
    }

    /**
     * 修改一条数据
     */
    public function editOne(array $data)
    {
        try {
            unset($data['file']);

            $imgs = $data['img'];
            unset($data['img']);

            $res = RepairstoreGoodsRepository::edit_one($data);

            foreach ($imgs as $key => &$value) {
                $value['goods_id'] = $data['id'];
                $value['sorts'] = $key + 1;

                RepairstoreGoodsImgRepository::edit_or_create(['id' => $value['id']], $value);//存入图片信息
            }

            if (empty($res)) {
                return hello_error('修改失败');
            }

            return hello_success('修改成功', $res);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 删除商品图片
     */
    public function delPic($data)
    {
        try {
            if ($data <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = RepairstoreGoodsImgRepository::delete_one($data);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');

        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 删除
     */
    public function delGoods($data)
    {
        try {
            if ($data <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = RepairstoreGoodsRepository::delete_one($data);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');

        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }


}